package adb;

import static adb.ADBView.screenWidth;
import static adb.OSK.adbOSK;
import static adb.OSK.commandOSK;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.RawImage;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class ScreenShotRealtimeLandscape100 extends SwingWorker<Void, String>{

    public static int rawImageWidth;
    public static int rawImageHeight;
    public static int SSRealTimeL100open=0;
    public static AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
//    BufferedImage rotateImage50;
    IDevice device0;
    //runCommandOnly r = new runCommandOnly();
    int firstX=0;
    int firstY=0;
    int mouseClick=0;
    
    @Override
    protected Void doInBackground() throws Exception {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        AndroidDebugBridge.init(false /* debugger support */);
        int count = 0;
        while (bridge.hasInitialDeviceList() == false) {
            try {
                Thread.sleep(100);
                count++;
            } catch (InterruptedException ex) {}
        }
        creationFrameScreenShot();
        return null;
        
    }
    public void creationFrameScreenShot(){
        
        final JFrame framess = new JFrame();
        final JLabel imageLabel100 = new JLabel();

        framess.addWindowListener(new WindowListener() {    
            public void windowOpened(WindowEvent e) {
                SSRealTimeL100open=1;
            }
            public void windowClosing(WindowEvent e) {
                SSRealTimeL100open=0;
            }
            public void windowClosed(WindowEvent e) {
                SSRealTimeL100open=0;
//                bridge=null;
                rawImageWidth=DISPOSE_ON_CLOSE;
                rawImageHeight=DISPOSE_ON_CLOSE;
//                device0=null;
                firstX=DISPOSE_ON_CLOSE;
                firstY=DISPOSE_ON_CLOSE;
                mouseClick=DISPOSE_ON_CLOSE;
                
                framess.removeAll();
                System.gc();
                framess.dispose();
                
            }
            public void windowIconified(WindowEvent e) {
            }
            public void windowDeiconified(WindowEvent e) {
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowDeactivated(WindowEvent e) {               
            }
        });        
        
        imageLabel100.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                
                firstX = e.getX();
                firstY = e.getY();
            }
        });
        imageLabel100.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                runCommandOnly r = new runCommandOnly();
                commandOSK = adbOSK+" shell input tap "+e.getX()+" "+e.getY();
                //System.out.print(commandOSK+mouseClick+"mouseClicked\n");
                r.execute();
            }
        });
        imageLabel100.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                int secondX = e.getX();
                int secondY = e.getY();
                if(Math.abs(secondX-firstX)>5||Math.abs(secondY-firstY)>5){
                runCommandOnly r = new runCommandOnly();
                commandOSK = adbOSK+" shell input swipe "+firstX+" "+firstY+" "+secondX+" "+secondY;
                //System.out.print(commandOSK+mouseClick+"mouseReleased\n");
                r.execute();
                }
            }
        });
        imageLabel100.setIcon(new ImageIcon(getScreenShot()));
        framess.setSize(rawImageHeight+10, rawImageWidth+35);
        framess.setLocation((screenWidth - (rawImageHeight+10)) / 2,0);
        framess.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        framess.setResizable(false);
        framess.setTitle("Screen Shot");
        framess.getContentPane().add(imageLabel100);
        framess.setVisible(true);
        while(true){
        framess.add(imageLabel100);
        imageLabel100.setIcon(new ImageIcon(getScreenShot()));
        
        framess.remove(imageLabel100);
        }
    }
    public BufferedImage getScreenShot(){
        IDevice[] devices = bridge.getDevices();
        RawImage rawImage = null;
        device0 = devices[0];
        try {
            rawImage = device0.getScreenshot();
            rawImageHeight = rawImage.height;
            rawImageWidth= rawImage.width;
        } catch (IOException ex) {}
        BufferedImage image = new BufferedImage(rawImageWidth,rawImageHeight,BufferedImage.TYPE_INT_ARGB);
        int index = 0;
        int IndexInc = rawImage.bpp >> 3;
        for (int y = 0; y < rawImage.height; y++) {
            for (int x = 0; x < rawImage.width; x++) {
                int value = rawImage.getARGB(index);
                index += IndexInc;
                image.setRGB(x, y, value);
            }
        }
        
        return rotateImage(image,3);
    }
    public static BufferedImage rotateImage(BufferedImage image, int numquadrants) {
      int w0 = image.getWidth();
    int h0 = image.getHeight();
    int w1 = w0;
    int h1 = h0;
    int centerX = w0 / 2;
    int centerY = h0 / 2;
    if (numquadrants % 2 == 1) {
        w1 = h0;
        h1 = w0;
    }
    if (numquadrants % 4 == 1) {
        centerX = h0 / 2;
        centerY = h0 / 2;
    } else if (numquadrants % 4 == 3) {
        centerX = w0 / 2;
        centerY = w0 / 2;
    }
    AffineTransform affineTransform = new AffineTransform();
    affineTransform.setToQuadrantRotation(numquadrants, centerX, centerY);
    AffineTransformOp opRotated = new AffineTransformOp(affineTransform,
    AffineTransformOp.TYPE_BILINEAR);
    BufferedImage transformedImage = new BufferedImage(w1, h1,image.getType());
    transformedImage = opRotated.filter(image, transformedImage);
    return transformedImage;
   }

}
